﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using Common = A911CareerKit.Common;
using FavoritesModel = A911CareerKit.Favorites.Model;
using DevExpress.XtraEditors.Repository;

namespace A911CareerKit
{
    public partial class MainForm : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        #region Favorites

        private void RenewFavoritesList()
        {
            repositoryFavorites.Items.Clear();
            foreach (FavoritesModel.Favorite favorite in Locator.Favorites)
            {
                repositoryFavorites.Items.Add(favorite);
            }
        }

        void Locator_OnFavoritesChanged(object sender, EventArgs args)
        {
            RenewFavoritesList();
        }

        #endregion

        public MainForm()
        {
            InitializeComponent();

            RenewFavoritesList();
            Locator.OnFavoritesChanged += new LocatorService.FavoritesSubscriberHandle(Locator_OnFavoritesChanged);

            webBrowser.Navigate(Properties.Settings.Default.StartURL);                
        }


        private void btnGetLinks_ItemClick(object sender, ItemClickEventArgs e)
        {
            Common.Dialogs.ComingSoon.JustShowDialog();
        }

        private void btnAddToFavorites_ItemClick(object sender, ItemClickEventArgs e)
        {
            string url = webBrowser.Url.ToString();
            string title = Common.HtmlHelper.GetWebPageTitle(url);

            if (string.IsNullOrEmpty(title)) title = url;

            Dialogs.AddFavoriteDialog dialog = new Dialogs.AddFavoriteDialog();
            dialog.Url = url;
            dialog.Title = title;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                title = dialog.Title;
                Locator.Favorites.Items.Add(new FavoritesModel.Favorite() { Title = title, Url = url });
                Locator.Favorites.Save();
                Locator.FavoritesChanged(this);
            }
                             
        }

        private void btnEditFavorites_ItemClick(object sender, ItemClickEventArgs e)
        {
            Common.Dialogs.ComingSoon.JustShowDialog();
        }


        private void btnMarketplace_ItemClick(object sender, ItemClickEventArgs e)
        {
            webBrowser.Navigate(Properties.Settings.Default.MarketplaceURL);
        }

        /// <summary>
        /// Navigate to selected  favorite
        /// </summary>
        private void NavigateFavorite()
        {
            FavoritesModel.Favorite favorite = beFavorites.EditValue as FavoritesModel.Favorite;
            if (favorite == null) return;
            webBrowser.Navigate(favorite.Url);
            
        }

        private void repositoryFavorites_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            NavigateFavorite();                
                
        }

        private void repositoryFavorites_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13) // Enter
            {
                e.Handled = true;
                NavigateFavorite();
            }
        }
    }
}